package recursos;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
 
public class ArraylistTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	ArrayList data;
    private String[] columnNames;
 
    public ArraylistTableModel() {
    }
 
    public ArraylistTableModel(ArrayList datos, String[] cols) {
        this.data = datos;
        int numCols = cols.length;
        columnNames = new String[numCols];
        System.arraycopy(cols, 0, columnNames, 0, numCols);
    }
 
    public int getColumnCount() {
 
        return columnNames.length;
    }
 
    public int getRowCount() {
 
        return data.size();
    }
 
    @Override
    public String getColumnName(int col) {
 
        return columnNames[col];
    }
 
    public Object getValueAt(int row, int col) {
 
        ArrayList colArrayList = (ArrayList) data.get(row);
//    System.out.println("Celda[" + row + "," + col + "]" + colArrayList.get(col).toString());
        return colArrayList.get(col);
    }
 
    @Override
    public Class getColumnClass(int i) {
        ArrayList colArrayList = (ArrayList) data.get(0);
        return colArrayList.get(i).getClass();
    }
 
    @Override
    public boolean isCellEditable(int row, int col) {
        return super.isCellEditable(row, col);
    }
 
     
 
    @Override
    public void setValueAt(Object value, int row, int col) {
        ArrayList aux=(ArrayList) data.get(row);
        if(value instanceof Date){//para el caso de fechas
            SimpleDateFormat formateador=new SimpleDateFormat("yyyy-M-d");
            value=formateador.format(value);
        }
        aux.set(col, value);
        data.set(row, aux);
        if (col != 0){
            fireTableCellUpdated(row, col);
        }        
    }
 
    @Override
    public void fireTableCellUpdated(int row, int col) {
        if(this.getValueAt(row, 0) == Boolean.FALSE){
            this.setValueAt(Boolean.TRUE, row, 0);            
        }
    }
 
 
}